package com.topsun.framework.common.mybatis.plugins.page;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.List;

/**
 * @author pipi
 */
@ApiModel("分页响应数据")
public class Pagination<E> {

    @ApiModelProperty("当前页")
    private Integer page;

    @ApiModelProperty("总页数")
    private Integer pages;

    @ApiModelProperty("总记录数")
    private Integer count;

    @ApiModelProperty("是否有下一页")
    private Boolean hasNext;

    @ApiModelProperty("数据")
    private List<E> data;

    public Pagination() {
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getPages() {
        return pages;
    }

    public void setPages(Integer pages) {
        this.pages = pages;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Boolean getHasNext() {
        return hasNext;
    }

    public void setHasNext(Boolean hasNext) {
        this.hasNext = hasNext;
    }

    public List<E> getData() {
        return data;
    }

    public void setData(List<E> data) {
        this.data = data;
    }

    public Pagination(PageInfo pageInfo, List<E> list) {
        if (pageInfo != null) {
            this.hasNext = pageInfo.hasNext();
            this.page = Math.toIntExact(pageInfo.getCurrent());
            this.pages = Math.toIntExact(pageInfo.getPages());
            this.count = Math.toIntExact(pageInfo.getTotal());
        }
        this.data = list;
    }

    @Override
    public String toString() {
        int size = data == null ? 0 : data.size();
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("page:").append(page);
        sb.append(",pages:").append(pages);
        sb.append(",count:").append(count);
        sb.append(",hasNext:").append(hasNext);
        sb.append(",listSize:").append(size);
        sb.append("}");
        return sb.toString();
    }
}